package com.desmart.renjiao.xmlparase;

import android.util.Xml;

import com.desmart.renjiao.bean.Announcement;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class AnnouncmentXmlUtils{

    public static List<Announcement> readXML(InputStream inputStream) {
        XmlPullParser parser = Xml.newPullParser();

        try {
            parser.setInput(inputStream, "UTF-8");
            int eventType = parser.getEventType();

            Announcement currentPerson = null;
            List<Announcement> announcementList = null;

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        announcementList = new ArrayList<>();
                        break;
                    case XmlPullParser.START_TAG:
                        String name = parser.getName();
                        if (name.equalsIgnoreCase("item")) {
                            currentPerson = new Announcement();
                        } else if (currentPerson != null) {
                            if (name.equalsIgnoreCase("cate1")) {
                                currentPerson.cate1 = parser.nextText();
                            } else if (name.equalsIgnoreCase("cate2")) {
                                currentPerson.cate2 = parser.nextText();
                            }else if (name.equalsIgnoreCase("level")){
                                currentPerson.title = parser.nextText();
                            } else if (name.equalsIgnoreCase("title")){
                                currentPerson.body = parser.nextText();
                            } else if (name.equalsIgnoreCase("author")){
                                currentPerson.author = parser.nextText();
                            } else if (name.equalsIgnoreCase("releaseTime")){
                                currentPerson.releaseTime = parser.nextText();
                            } else if (name.equalsIgnoreCase("link")){
                                currentPerson.link = parser.nextText();
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (parser.getName().equalsIgnoreCase("item")
                                && currentPerson != null) {
                            announcementList.add(currentPerson);
                            currentPerson = null;
                        }
                        break;
                }
                eventType = parser.next();
            }
            inputStream.close();
            return announcementList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<Announcement> readXMLtheNews(InputStream inputStream) {
        XmlPullParser parser = Xml.newPullParser();

        try {
            parser.setInput(inputStream, "UTF-8");
            int eventType = parser.getEventType();

            Announcement currentPerson = null;
            List<Announcement> announcementList = null;

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        announcementList = new ArrayList<>();
                        break;
                    case XmlPullParser.START_TAG:
                        String name = parser.getName();
                        if (name.equalsIgnoreCase("item")) {
                            currentPerson = new Announcement();
                        } else if (currentPerson != null) {
                            if (name.equalsIgnoreCase("cate1")) {
                                currentPerson.cate1 = parser.nextText();
                            } else if (name.equalsIgnoreCase("cate2")) {
                                currentPerson.cate2 = parser.nextText();
                            }else if (name.equalsIgnoreCase("title")){
                                currentPerson.title = parser.nextText();
                            } else if (name.equalsIgnoreCase("author")){
                                currentPerson.author = parser.nextText();
                            } else if (name.equalsIgnoreCase("releaseTime")){
                                currentPerson.releaseTime = parser.nextText();
                            } else if (name.equalsIgnoreCase("link")){
                                currentPerson.link = parser.nextText();
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (parser.getName().equalsIgnoreCase("item")
                                && currentPerson != null) {
                            announcementList.add(currentPerson);
                            currentPerson = null;
                        }
                        break;
                }
                eventType = parser.next();
            }
            inputStream.close();
            return announcementList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
